<template>
  <div class="pxy20">
    <ElForm
      ref="icRefs"
      :model="articleContent"
      class="myfrom"
      :rules="articleICRules"
      label-width="80px"
      label-position="left"
    >
      <ElFormItem label="文章标题" prop="title">
        <ElInput v-model="articleContent.title" placeholder="文章标题" style="width: 400px" />
      </ElFormItem>
      <ElFormItem label="文章类别" prop="type">

        <ElInput v-model="articleContent.type" placeholder="文章类别" style="width: 400px" />
      </ElFormItem>
      <ElFormItem label="文章概述" prop="articleSummary">
        <ElInput
          v-model="articleContent.articleSummary"
          class="summary"
          placeholder="文章概述"
          style="width: 400px"
          maxlength="150"
          show-word-limit
        />
      </ElFormItem>

      <ElFormItem label="日期" prop="articleDate">
        <el-date-picker
          v-model="createDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期"
        />
        <el-time-picker v-model="createTime" placeholder="选择时间" format="HH:mm:ss" />
      </ElFormItem>
      <!-- action=" https://api.ukshuxi.com/api/file/uploadFile" -->
      <!-- :action="baseUrl+'/api/file/uploadFile'" -->
      <ElFormItem label="封面" prop="image">
        <el-upload
          class="avatar-uploader"
          action="https://api.ukshuxi.com/api/file/uploadFile"
          :show-file-list="false"
          :on-success="uploadSuccess"
        >
          <img v-if="articleContent.image" :src="baseUrl + articleContent.image" class="avatar">
          <i v-else class="el-icon-plus avatar-uploader-icon" />
        </el-upload>
      </ElFormItem>
      <ElFormItem label="是否设置为推荐位">
        <el-radio v-model="articleContent.isRecommended" :label="true">是</el-radio>
        <el-radio v-model="articleContent.isRecommended" :label="false">否</el-radio>
      </ElFormItem>
      <ElFormItem label="正文" prop="content"><br>
        <editorVue v-model="articleContent.content" />
      </ElFormItem>
    </ElForm>
    <br>
    <ElButton type="primary" @click="submitBtn">发布</ElButton>
  </div>
</template>
<script>
import editorVue from '@/views/article/editor.vue'
import { articleICRules } from '@/utils/formVerify'
import { ARTICLE_ADD, ARTICLE_EDIT } from '@/api/manage/article'

export default {
  name: 'ManageArticleEdit',
  components: { editorVue },
  data() {
    return {
      articleContent: {
        title: '',
        type: '',
        articleSummary: '',
        content: '',
        image: '',
        articleCreateTime: '',
        isRecommended: false
      },
      createDate: '',
      createTime: '',
      baseUrl: 'https://api.ukshuxi.com',
      articleICRules,
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    }
  },
  mounted() {
    const row = this.$route.params
    if (Object.keys(row).length !== 0) {
      this.articleContent = row
      this.createDate = this.articleContent.articleCreateTime.slice(0, 11)
      this.createTime = new Date(this.articleContent.articleCreateTime)
    }
  },
  methods: {
    submitBtn() {
      if (!this.createDate || !this.createTime) {
        this.$message.warning('请选择日期和时间')
      }
      this.$refs['icRefs'].validate((i) => {
        this.articleContent.type = this.articleContent.type.trim()
        this.articleContent.articleCreateTime = this.createDate + ' ' + this.createTime.getHours() + ':' + this.createTime.getMinutes()
        if (i) {
          this.articleContent.id
            ? ARTICLE_EDIT(this.articleContent).then((res) => {
              this.apiRes(res)
            })
            : ARTICLE_ADD(this.articleContent).then((res) => {
              this.apiRes(res)
            })
        }
      })
    },

    uploadSuccess(res, file, fileList) {
      this.articleContent.image =
        '/drdImg/' + res.data.fileName
      this.$message({
        message:
          +res.code === 200 ? '封面上传成功!' : '封面上传失败，请联系系统管理员!',
        type: +res.code === 200 ? 'success' : 'error'
      })
    },

    apiRes(res) {
      const i = +res['result'] === 0

      this.$message({
        message: i
          ? this.articleContent.id
            ? '文章更新成功!'
            : '文章增加成功！'
          : '文章编辑失败!' + res.msg,
        type: i ? 'success' : 'error'
      })
      i ? this.$refs['icRefs'].resetFields() : ''
      i && this.$router.push({ name: 'abroadArticle' })
    }
  }
}
</script>
<style lang="scss" scoped>

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.summary {
  padding-right: 60px;
}
.el-date-editor {
  margin-right: 10px;
}
</style>
